package br.fatec.ads.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.fatec.ads.dao.AreaDAO;
import br.fatec.ads.dao.ProcessoSeletivoDAO;
import br.fatec.ads.dao.VagaDAO;
import br.fatec.ads.exception.AreaException;
import br.fatec.ads.exception.DataBaseException;
import br.fatec.ads.exception.ProcessoSeletivoException;
import br.fatec.ads.exception.VagaException;
import br.fatec.ads.model.Area;
import br.fatec.ads.model.ProcessoSeletivo;
import br.fatec.ads.model.Vaga;

public class VagaIncluir extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public VagaIncluir() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			List listaProcessoSeletivo = new ArrayList();
			List listaArea = new ArrayList();
			HttpSession session = request.getSession(false);
			
			Area area = new Area();
			
			ProcessoSeletivoDAO processoSeletivoDAO = new ProcessoSeletivoDAO();
			AreaDAO areaDAO = new AreaDAO();
		
			listaProcessoSeletivo = processoSeletivoDAO.listarProcessoSeletivo(null, null);
			listaArea = areaDAO.listarArea(area);
			
			session.setAttribute("listaComboProcessoSeletivo", listaProcessoSeletivo);
			session.setAttribute("listaComboArea", listaArea);
			
			request.getRequestDispatcher("cadvaga_i.jsp").forward(request, response);
		} catch (ProcessoSeletivoException e) {
			e.printStackTrace();
		} catch (AreaException e) {
			e.printStackTrace(); 
		} catch (DataBaseException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			
			Vaga vaga = new Vaga();
			ProcessoSeletivo processoSeletivo = new ProcessoSeletivo();
			Area area = new Area();
			
			processoSeletivo.setIdProcessoSeletivo(Integer.parseInt(request.getParameter("cmbProcessoSeletivo")));
			
			area.setIdArea(Integer.parseInt(request.getParameter("cmbArea")));
			
			vaga.setProcessoSeletivo(processoSeletivo);
			vaga.setArea(area);
			vaga.setCargoVaga(request.getParameter("txtCargoVaga"));
			vaga.setDescVaga(request.getParameter("txtDescVaga"));
			vaga.setQtdeVaga(Integer.parseInt(request.getParameter("txtQtdeVaga")));
			vaga.setUsuario(session.getAttribute("usuarioDesc").toString());
			
			VagaDAO vagaDAO = new VagaDAO();
			
			vagaDAO.incluirVaga(vaga);
			
			request.getRequestDispatcher("VagaPesquisar?acao=SUCESSO_INCLUIR").forward(request, response);
		} catch (VagaException e) {
			e.printStackTrace();
		} catch (DataBaseException e) {
			e.printStackTrace();
		}
	}
}